package com.hancher.common.javautil;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 描述 : 字符串工具类 <br/>
 * 时间 : 2023/12/11 11:02 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class StringUtil {

    /**
     * 获取对象字符串
     *
     * @param obj 1.200
     * @return 1.2
     */
    public static String getObjStr(Object obj) {
        return getObjStr(obj, "");
    }

    /**
     * 获取对象字符串
     *
     * @param obj        1.200
     * @param defaultStr
     * @return 1.2
     */
    public static String getObjStr(Object obj, String defaultStr) {
        if (obj == null) {
            return defaultStr;
        }
        if (obj instanceof Double) {
            if ((BigDecimal.valueOf((Double) obj).compareTo(BigDecimal.ZERO) == 0)) {
                return "0";
            }
            return (BigDecimal.valueOf((Double) obj).stripTrailingZeros().toPlainString());
        } else if (obj instanceof BigDecimal) {
            if (((BigDecimal) obj).compareTo(BigDecimal.ZERO) == 0) {
                return "0";
            }
            return ((BigDecimal) obj).stripTrailingZeros().toPlainString();
        } else if (obj instanceof String) {
            if (TextUtils.isEmpty((CharSequence) obj)) {
                return defaultStr;
            }
        }
        return obj + "";
    }

    /**
     * 列表转字符串
     *
     * @param strList 1,2,3
     * @return "1,2,3"
     */
    public static String strList2str(List<String> strList) {
        StringBuffer value = new StringBuffer();
        for (String item : strList) {
            if (EmptyUtil.isEmpty(value)) {
                value.append(item);
            } else {
                value.append(",").append(item);
            }
        }
        return value.toString();
    }

}
